
#include "dev_oc.h"
#include "page_base.h"
#include "img_common.h"
#include "pt_ipc.h"
#include "pt_fs.h"
#include "sys_font.h"

static char* icon_close[1] =
{
    "curtain_ic_close.png",
};
static char* icon_open[1] =
{
    "curtain_ic_open.png",
};
static char* icon_stop[1] =
{
    "curtain_ic_stop.png",
};

static int dev_oc_open(ui_dev_oc_t* oc)
{
    oc_http_req_t http_req;    
    memset(&http_req,0,sizeof(oc_http_req_t));

    char body[128] = {0};
    memset(body,0,sizeof(body));

    // snprintf(body, 128, "","");

    snprintf(http_req.devid,sizeof(http_req.devid),"%s",oc->data.devid);
    snprintf(http_req.body, sizeof(http_req.body), "%s",body);

    debug_d("devid=%s\n",http_req.devid);
    debug_d("body =%s\n",http_req.body);

    http_req.action = CMD_OC_OPEN;
    pt_ipc_send_msg(IPC_MSG_OC_CTL,(void*)&http_req,sizeof(oc_http_req_t));

    return 0;
}

static int dev_oc_close(ui_dev_oc_t* oc)
{
    oc_http_req_t http_req;    
    memset(&http_req,0,sizeof(oc_http_req_t));

    char body[128] = {0};
    memset(body,0,sizeof(body));

    // snprintf(body, 128, "","");

    snprintf(http_req.devid,sizeof(http_req.devid),"%s",oc->data.devid);
    snprintf(http_req.body, sizeof(http_req.body), "%s",body);

    debug_d("devid=%s\n",http_req.devid);
    debug_d("body =%s\n",http_req.body);

    http_req.action = CMD_OC_CLOSE;
    pt_ipc_send_msg(IPC_MSG_OC_CTL,(void*)&http_req,sizeof(oc_http_req_t));

    return 0;
}

static int dev_oc_stop(ui_dev_oc_t* oc)
{
    oc_http_req_t http_req;    
    memset(&http_req,0,sizeof(oc_http_req_t));

    char body[128] = {0};
    memset(body,0,sizeof(body));

    // snprintf(body, 128, "","");

    snprintf(http_req.devid,sizeof(http_req.devid),"%s",oc->data.devid);
    snprintf(http_req.body, sizeof(http_req.body), "%s",body);

    debug_d("devid=%s\n",http_req.devid);
    debug_d("body =%s\n",http_req.body);

    http_req.action = CMD_OC_STOP;
    pt_ipc_send_msg(IPC_MSG_OC_CTL,(void*)&http_req,sizeof(oc_http_req_t));

    return 0;
}

static int dev_oc_open_clicked(ui_dev_oc_t* oc)
{
    if(!oc->ui.time)
        return 0;

    oc->data.old_val    = oc->data.cur_val;
    oc->data.target_val = oc->data.max;

    oc->data.workstatus   = DEV_OC_OPENING;

    dev_oc_open(oc);

    return 0;
}

static void dev_oc_left_event_callback(lv_event_t* event)
{
    ui_dev_oc_t* oc = NULL;
    oc = (ui_dev_oc_t*)lv_event_get_user_data(event);

    switch(event->code){
    case LV_EVENT_CLICKED:
    {
        dev_oc_open_clicked(oc);
        break;
    }
    default:break;
    }
}

static int dev_oc_stop_clicked(ui_dev_oc_t* oc)
{
    char txt_buff[128];

    if(!oc->ui.time)
        return 0;
    if(!oc->ui.bar_txt)
        return 0;

    oc->data.old_val    = oc->data.cur_val;
    oc->data.target_val = oc->data.cur_val;

    oc->data.workstatus = DEV_OC_STOP;

    dev_oc_stop(oc);

    return 0;
}


static void dev_oc_stop_event_callback(lv_event_t* event)
{
    ui_dev_oc_t* oc = NULL;
    oc = (ui_dev_oc_t*)lv_event_get_user_data(event);

    switch(event->code){
    case LV_EVENT_CLICKED:
    {
        dev_oc_stop_clicked(oc);
        break;
    }
    default:break;
    }
}
static int dev_oc_close_clicked(ui_dev_oc_t* oc)
{
    if(!oc->ui.time)
        return 0;

    oc->data.old_val    = oc->data.cur_val;
    oc->data.target_val = oc->data.min;

    oc->data.workstatus = DEV_OC_CLOEING;

    dev_oc_close(oc);

    return 0;
}


static void dev_oc_right_event_callback(lv_event_t* event)
{

    ui_dev_oc_t* oc       = NULL;
    oc = (ui_dev_oc_t*)lv_event_get_user_data(event);

    switch(event->code){
    case LV_EVENT_CLICKED:
    {
        dev_oc_close_clicked(oc);
        break;
    }
    default:break;
    }
}

static void dev_oc_bar_event_callback(lv_event_t* event)
{
    lv_coord_t x = 0;
    lv_area_t   bar_area;
    lv_point_t  lv_point;
    lv_indev_t* indev = NULL;

    ui_dev_oc_t* oc       = NULL;
    oc = (ui_dev_oc_t*)lv_event_get_user_data(event);

    int w = 0;
    int h = 0;
    int t_w = 0;
    int t_h = 0;
    int val = 0;

    char txt_buff[128];

    if(oc->data.is_notrip)
    {
        return ;
    }

    indev = lv_indev_get_act();
    if(!indev){
        return ;
    }
    if(!oc->ui.bar){
        return ;
    }
    if(!oc->ui.bar_txt){
        return ;
    }
    if(!oc->ui.bar_head){
        return ;
    }
    if(!oc->ui.time){
        return ;
    }

    lv_indev_get_point(indev, &lv_point);
    lv_obj_get_coords(oc->ui.bar, &bar_area);

    w   = bar_area.x2 - bar_area.x1 + 1;
    h   = bar_area.y2 - bar_area.y1 + 1;
    t_w = lv_point.x - bar_area.x1 + 1;
    t_h = lv_point.y - bar_area.y1 + 1;

    //debug_d("t_w:w=%d:%d\n",t_w,w);
    //debug_d("t_h:h=%d:%d\n",t_h,h);

    if(t_w < 0)
    {
       t_w = 0;
    }
    if(t_w > w)
    {
       t_w = w;
    }

    switch(event->code){
    case LV_EVENT_CLICKED:
    {
        debug_d("old_val   =%d\n",oc->data.old_val);
        debug_d("target_val=%d\n",oc->data.target_val);

        break;
    }
    case LV_EVENT_RELEASED:
    {
        debug_d("old_val   =%d\n",oc->data.old_val);
        debug_d("target_val=%d\n",oc->data.target_val);

        root_gesture_enable();
        break;
    }
    case LV_EVENT_PRESSED:
    {
        root_gesture_disable();
        oc->data.old_val = oc->data.cur_val;
        break;
    }
    case LV_EVENT_PRESSING:
    {
        val = t_w*oc->data.max/w;
        oc->data.target_val = val;
        //*******************************************************
        lv_bar_set_value(oc->ui.bar,oc->data.target_val, LV_ANIM_OFF);
        //*******************************************************

        //*******************************************************
        memset(txt_buff,0,sizeof(txt_buff));
        snprintf(txt_buff,128,"已打开%d%%",oc->data.target_val);
        lv_label_set_text(oc->ui.bar_txt,txt_buff);
        //*******************************************************
        x = (oc->data.target_val*320)/oc->data.max + OC_BAR_POS_X - 8;
        lv_obj_set_pos(oc->ui.bar_head,x,149);
        //*******************************************************

        break;
    }
    default:break;
    }
}

static int ui_dev_oc_auto_update(ui_dev_oc_t* oc)
{
    lv_coord_t x = 0;
    char txt_buff[128];
    static int cur_val     = 0;
    static int workstatus  = 0;

    if(!oc){
        return -1;
    }

    // debug_d("cur_val   =%d\n",oc->data.cur_val);
    // debug_d("target_val=%d\n",oc->data.target_val);

    if(oc->data.target_val > oc->data.cur_val)
    {
        oc->data.workstatus = DEV_OC_OPENING;
        oc->data.cur_val++;

        if(oc->data.is_notrip)
        {
            if(oc->data.cur_val == oc->data.max)
            {
                oc->data.is_notrip = 0;
                debug_d("create trip ok\n");
            }
        }
    }
    else if(oc->data.target_val < oc->data.cur_val)
    {
        oc->data.workstatus = DEV_OC_CLOEING;
        oc->data.cur_val--;
    }
    else{
        oc->data.workstatus = DEV_OC_STOP;
    }

    if((workstatus != oc->data.workstatus)||
       (cur_val != oc->data.cur_val))
    {
        workstatus = oc->data.workstatus;
        cur_val    = oc->data.cur_val;
    }else{
        return -1;
    }

    memset(txt_buff,0,sizeof(txt_buff));

    if(oc->data.is_notrip)
    {
        lv_obj_set_style_bg_opa(oc->ui.bar, LV_OPA_100, LV_PART_MAIN);
        lv_obj_set_style_bg_color(oc->ui.bar, lv_color_make(0x80, 0x80, 0x80), LV_PART_MAIN);
    }
    else{
        lv_obj_set_style_bg_opa(oc->ui.bar, LV_OPA_10, LV_PART_MAIN);
        lv_obj_set_style_bg_color(oc->ui.bar, lv_color_make(0xFF, 0xFF, 0xFF), LV_PART_MAIN);
    }

    if(oc->data.is_notrip)
    {
        snprintf(txt_buff,sizeof(txt_buff),"暂无行程");
        lv_label_set_text(oc->ui.bar_txt,txt_buff);
    }
    else if(oc->data.workstatus == DEV_OC_STOP)
    {
        snprintf(txt_buff,sizeof(txt_buff),"已打开%d%%",oc->data.cur_val);
        lv_label_set_text(oc->ui.bar_txt,txt_buff);
    }
    else if(oc->data.workstatus == DEV_OC_OPENING)
    {
        snprintf(txt_buff,sizeof(txt_buff),"正在打开");
        lv_label_set_text(oc->ui.bar_txt,txt_buff);
    }
    else if(oc->data.workstatus == DEV_OC_CLOEING)
    {
        snprintf(txt_buff,sizeof(txt_buff),"正在关闭");
        lv_label_set_text(oc->ui.bar_txt,txt_buff);   
    }
    //*******************************************************
    lv_bar_set_value(oc->ui.bar,oc->data.cur_val, LV_ANIM_OFF);
    //*******************************************************
    x = (oc->data.cur_val*320)/oc->data.max + OC_BAR_POS_X - 8;
    lv_obj_set_pos(oc->ui.bar_head,x,149);
    //*******************************************************

    return 0;
}

static void ui_dev_oc_time_callback(struct _lv_timer_t * t)
{
    ui_dev_oc_t* oc       = NULL;
    oc = (ui_dev_oc_t*)t->user_data;

    ui_dev_oc_auto_update(oc);
}


int ui_dev_oc_hide(ui_dev_oc_t* oc)
{
    if(!oc){
        return -1;
    }

    lv_obj_add_flag(oc->ui.bg_img,LV_OBJ_FLAG_HIDDEN);

    lv_obj_add_flag(oc->ui.left,LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(oc->ui.left_img,LV_OBJ_FLAG_HIDDEN);

    lv_obj_add_flag(oc->ui.btn_stop,LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(oc->ui.btn_stop_img,LV_OBJ_FLAG_HIDDEN);

    lv_obj_add_flag(oc->ui.right,LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(oc->ui.right_img,LV_OBJ_FLAG_HIDDEN);

    lv_obj_add_flag(oc->ui.bar,LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(oc->ui.bar_head,LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(oc->ui.bar_txt,LV_OBJ_FLAG_HIDDEN);

    lv_timer_pause(oc->ui.time);

    return 0;
}

int ui_dev_oc_show(ui_dev_oc_t* oc)
{
    if(!oc){
        return -1;
    }

    lv_obj_clear_flag(oc->ui.bg_img,LV_OBJ_FLAG_HIDDEN);

    lv_obj_clear_flag(oc->ui.left,LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(oc->ui.left_img,LV_OBJ_FLAG_HIDDEN);

    lv_obj_clear_flag(oc->ui.btn_stop,LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(oc->ui.btn_stop_img,LV_OBJ_FLAG_HIDDEN);

    lv_obj_clear_flag(oc->ui.right,LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(oc->ui.right_img,LV_OBJ_FLAG_HIDDEN);

    lv_obj_clear_flag(oc->ui.bar,LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(oc->ui.bar_head,LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(oc->ui.bar_txt,LV_OBJ_FLAG_HIDDEN);

    lv_timer_resume(oc->ui.time);

    return 0;
}


int ui_dev_oc_create(lv_obj_t* parent,void* ui_data)
{
    lv_coord_t pw = 0;
    lv_coord_t ph = 0;
    lv_coord_t x  = 0;
    char txt_buff[256];

    lv_obj_t*   superview = parent;
    ui_dev_oc_t* oc       = (ui_dev_oc_t*)ui_data;


    debug_d("superview=%x\n",superview);
    debug_d("oc=%x\n",oc);

    pw = lv_disp_get_hor_res(NULL);
    ph = lv_disp_get_ver_res(NULL);
    //*****************************************************
    oc->ui.bg_img = lv_img_create(superview);
    #if 0
    lv_img_set_src(oc->ui.bg_img,pt_fs_get("BG_Blue.png"));
    #else
    lv_img_set_src(oc->ui.bg_img,&BG_Blue);
    #endif
    lv_obj_set_pos(oc->ui.bg_img,0,0);
    //***************************************************************************
    oc->ui.left = lv_obj_create(superview);

    lv_obj_set_pos(oc->ui.left,26,191);
    lv_obj_set_size(oc->ui.left,96,96);

	lv_obj_set_style_radius(oc->ui.left, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(oc->ui.left, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(oc->ui.left, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(oc->ui.left, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(oc->ui.left, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(oc->ui.left, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(oc->ui.left, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(oc->ui.left, 0,0);

    #if 1
    lv_obj_set_style_bg_opa(oc->ui.left,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(oc->ui.left,LV_OPA_100,0);
    lv_obj_set_style_bg_color(oc->ui.left,lv_color_make(0xff,0,0),0);
    #endif

    lv_obj_add_event_cb(oc->ui.left, dev_oc_left_event_callback, LV_EVENT_ALL,(void*)oc);
    //*****************************************
    oc->ui.left_img = lv_img_create(oc->ui.left);
    lv_img_set_src(oc->ui.left_img,pt_fs_get(icon_open[0]));
    lv_obj_set_pos(oc->ui.left_img,0,0);
    //***********************************************************************
    oc->ui.btn_stop = lv_obj_create(superview);

    lv_obj_set_pos(oc->ui.btn_stop,179,191);
    lv_obj_set_size(oc->ui.btn_stop,96,96);

	lv_obj_set_style_radius(oc->ui.btn_stop, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(oc->ui.btn_stop, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(oc->ui.btn_stop, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(oc->ui.btn_stop, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(oc->ui.btn_stop, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(oc->ui.btn_stop, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(oc->ui.btn_stop, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(oc->ui.btn_stop, 0,0);

    #if 1
    lv_obj_set_style_bg_opa(oc->ui.btn_stop,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(oc->ui.btn_stop,LV_OPA_100,0);
    lv_obj_set_style_bg_color(oc->ui.btn_stop,lv_color_make(0xff,0,0),0);
    #endif

    lv_obj_add_event_cb(oc->ui.btn_stop, dev_oc_stop_event_callback, LV_EVENT_ALL,(void*)oc);
    //*****************************************
    oc->ui.btn_stop_img = lv_img_create(oc->ui.btn_stop);
    lv_img_set_src(oc->ui.btn_stop_img,pt_fs_get(icon_stop[0]));
    lv_obj_set_pos(oc->ui.btn_stop_img,0,0);
    //*****************************************************************************
    oc->ui.right = lv_obj_create(superview);

    lv_obj_set_pos(oc->ui.right,333,191);
    lv_obj_set_size(oc->ui.right,96,96);

	lv_obj_set_style_radius(oc->ui.right, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(oc->ui.right, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(oc->ui.right, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(oc->ui.right, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(oc->ui.right, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(oc->ui.right, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(oc->ui.right, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(oc->ui.right, 0,0);

    #if 1
    lv_obj_set_style_bg_opa(oc->ui.right,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(oc->ui.right,LV_OPA_100,0);
    lv_obj_set_style_bg_color(oc->ui.right,lv_color_make(0xff,0,0),0);
    #endif

    lv_obj_add_event_cb(oc->ui.right, dev_oc_right_event_callback, LV_EVENT_ALL,(void*)oc);
    //*****************************************
    oc->ui.right_img = lv_img_create(oc->ui.right);
    lv_img_set_src(oc->ui.right_img,pt_fs_get(icon_close[0]));
    lv_obj_set_pos(oc->ui.right_img,0,0);
    //*****************************************************************************
    //bar

	oc->ui.bar = lv_bar_create(superview);

	lv_obj_set_pos(oc->ui.bar,OC_BAR_POS_X,153);
	lv_obj_set_size(oc->ui.bar,320,16);

    lv_obj_set_style_radius(oc->ui.bar, 40, LV_PART_MAIN);
	lv_obj_set_style_border_width(oc->ui.bar, 0, LV_PART_MAIN);
    lv_obj_set_style_border_opa(oc->ui.bar, LV_OPA_0, LV_PART_MAIN);
	lv_obj_set_style_border_color(oc->ui.bar, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);

	lv_bar_set_range(oc->ui.bar,oc->data.min,oc->data.max);
	lv_bar_set_mode(oc->ui.bar, LV_BAR_MODE_SYMMETRICAL);

#if 0
	lv_obj_set_style_bg_opa(oc->ui.bar, LV_OPA_0, LV_PART_MAIN);
#else
    if(oc->data.is_notrip)
    {
        lv_obj_set_style_bg_opa(oc->ui.bar, LV_OPA_100, LV_PART_MAIN);
        lv_obj_set_style_bg_color(oc->ui.bar, lv_color_make(0x80, 0x80, 0x80), LV_PART_MAIN);
    }
    else{
        lv_obj_set_style_bg_opa(oc->ui.bar, LV_OPA_10, LV_PART_MAIN);
        lv_obj_set_style_bg_color(oc->ui.bar, lv_color_make(0xFF, 0xFF, 0xFF), LV_PART_MAIN);
    }
#endif

	lv_obj_set_style_bg_opa(oc->ui.bar, LV_OPA_100, LV_PART_INDICATOR);
	lv_obj_set_style_bg_color(oc->ui.bar, lv_color_make(0x4e, 0xb2, 0xff), LV_PART_INDICATOR);
    lv_obj_set_style_bg_grad_color(oc->ui.bar,lv_color_make(0x26, 0x7a, 0xff), LV_PART_INDICATOR);
    lv_obj_set_style_bg_grad_dir(oc->ui.bar,LV_GRAD_DIR_HOR,LV_PART_INDICATOR);

	lv_bar_set_value(oc->ui.bar,oc->data.cur_val, LV_ANIM_ON);

    lv_obj_add_event_cb(oc->ui.bar, dev_oc_bar_event_callback, LV_EVENT_ALL,(void*)oc);
    //***************************************************************
    //***************************************************************
    oc->ui.bar_txt = lv_label_create(superview);
    lv_obj_set_pos(oc->ui.bar_txt,74,59);
    lv_obj_set_size(oc->ui.bar_txt,325,70);
    lv_obj_set_style_text_align(oc->ui.bar_txt,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(oc->ui.bar_txt,LV_OPA_80,LV_PART_MAIN);
    lv_obj_set_style_text_color(oc->ui.bar_txt,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN);
    lv_obj_set_style_text_font(oc->ui.bar_txt, lv_font_get(60), LV_STATE_DEFAULT);

    #if 1
    lv_obj_set_style_bg_opa(oc->ui.bar_txt,LV_OPA_0, LV_PART_MAIN);
    #else
	lv_obj_set_style_bg_opa(oc->ui.bar_txt, LV_OPA_100,0);
	lv_obj_set_style_bg_color(oc->ui.bar_txt, lv_color_make(0xFF,0,0),0);
    #endif

    memset(txt_buff,0,sizeof(txt_buff));

    if(oc->data.is_notrip)
    {
        snprintf(txt_buff,sizeof(txt_buff),"暂无行程");
        lv_label_set_text( oc->ui.bar_txt,txt_buff);
    }
    else{
        snprintf(txt_buff,sizeof(txt_buff),"已打开%d%%",oc->data.cur_val);
        lv_label_set_text( oc->ui.bar_txt,txt_buff);
    }
    //***************************************************************
    oc->ui.bar_head = lv_obj_create(superview);

    x = (oc->data.cur_val*320)/oc->data.max + OC_BAR_POS_X - 8;

    lv_obj_set_pos(oc->ui.bar_head,x,149);
    lv_obj_set_size(oc->ui.bar_head,16,24);

	lv_obj_set_style_radius(oc->ui.bar_head,4, LV_PART_MAIN);
	lv_obj_set_style_border_width(oc->ui.bar_head, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(oc->ui.bar_head, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(oc->ui.bar_head, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(oc->ui.bar_head, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(oc->ui.bar_head, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(oc->ui.bar_head, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(oc->ui.bar_head, 0,0);

    #if 0
    lv_obj_set_style_bg_opa(oc->ui.bar_head,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(oc->ui.bar_head,LV_OPA_100,0);
    lv_obj_set_style_bg_color(oc->ui.bar_head,lv_color_make(0xff,0xff,0xff),0);
    #endif

    lv_obj_add_event_cb(oc->ui.bar_head, dev_oc_bar_event_callback, LV_EVENT_ALL,(void*)oc);
    //***************************************************************
    oc->ui.time = lv_timer_create(ui_dev_oc_time_callback,200,(void*)oc);

    //***************************************************************
    return 0;
}

int ui_dev_oc_destroy(void* ui_data)
{
    ui_dev_oc_t* oc  = (ui_dev_oc_t*)ui_data;

    if(oc->ui.bg_img){
        lv_obj_del(oc->ui.bg_img);
        oc->ui.bg_img = NULL;
    }
    if(oc->ui.left){
        lv_obj_del(oc->ui.left);
        oc->ui.left  = NULL;
        oc->ui.left_img  = NULL;
    }
    if(oc->ui.btn_stop){
        lv_obj_del(oc->ui.btn_stop);
        oc->ui.btn_stop  = NULL;
        oc->ui.btn_stop_img  = NULL;
    }
    if(oc->ui.right){
        lv_obj_del(oc->ui.right);
        oc->ui.right  = NULL;
        oc->ui.right_img  = NULL;
    }
    if(oc->ui.bar){
        lv_obj_del(oc->ui.bar);
        oc->ui.bar  = NULL;
    }
    if(oc->ui.bar_txt){
        lv_obj_del(oc->ui.bar_txt);
        oc->ui.bar_txt  = NULL;
    }
    if(oc->ui.bar_head){
        lv_obj_del(oc->ui.bar_head);
        oc->ui.bar_head  = NULL;
    }
    if(oc->ui.time){
        lv_timer_del(oc->ui.time);
        oc->ui.time  = NULL;
    }

    return 0;
}


static int ui_oc_create(lv_obj_t* parent,void* ui_data)
{
    lv_obj_t* superview = parent;
    ui_dev_oc_t* oc     = (ui_dev_oc_t*)ui_data;


    ui_dev_oc_create(parent,ui_data);
    ui_dev_off_create(superview,&oc->ui.dev_off);
    ui_dev_off_line_create(superview,&oc->ui.dev_off_line);

    switch(oc->data.status){
    case DEV_WORK:
    {
        ui_dev_oc_show(oc);
        ui_dev_off_hide(&oc->ui.dev_off);
        ui_dev_off_line_hide(&oc->ui.dev_off_line);
        break;
    }
    case DEV_OFF:
    {
        ui_dev_oc_hide(oc);
        ui_dev_off_show(&oc->ui.dev_off);
        ui_dev_off_line_hide(&oc->ui.dev_off_line);
        break;
    }
    case DEV_OFF_LINE:
    {
        ui_dev_oc_hide(oc);
        ui_dev_off_hide(&oc->ui.dev_off);
        ui_dev_off_line_show(&oc->ui.dev_off_line);
        break;
    }
    default:break;
    }

    return 0;
}

static int ui_oc_destroy(void* ui_data)
{
    ui_dev_oc_t* oc = (ui_dev_oc_t*)ui_data;

	ui_dev_off_destroy(&oc->ui.dev_off);
    ui_dev_off_line_destroy(&oc->ui.dev_off_line);
    ui_dev_oc_destroy(ui_data);

    return 0;
}

static void dev_oc_on_custom_attr_config(page_base_t* self)
{

}
static void dev_oc_on_view_load(page_base_t* self)
{
    
}
static void dev_oc_on_view_did_load(page_base_t* self)
{
    
}
static void dev_oc_on_view_will_appear(page_base_t* self)
{

}
static void dev_oc_on_view_did_appear(page_base_t* self)
{

}
static void dev_oc_on_view_will_disappear(page_base_t* self)
{

}
static void dev_oc_on_view_did_disappear(page_base_t* self)
{

}
static void dev_oc_on_view_did_unload(page_base_t* self)
{
    // ui_oc_destroy(self);
}

dev_info_t* dev_oc_reg(ui_oc_data_t* dev)
{
    int len = 0;
    ui_dev_oc_t*    dev_oc   = NULL;
    page_vtable_t*  method   = NULL;
    dev_info_t*     devinfo  = NULL;

    if(!dev){
        return NULL;
    }

    devinfo = (dev_info_t*)malloc(sizeof(dev_info_t));
    if(!devinfo){
        debug_d("merr\n");
        return NULL;
    }

    dev_oc = (ui_dev_oc_t*)malloc(sizeof(ui_dev_oc_t));
    if(!dev_oc){
        debug_d("merr\n");
        return NULL;
    }

    memset(dev_oc,0,sizeof(ui_dev_oc_t));

    method = (page_vtable_t*)malloc(sizeof(page_vtable_t));
    if(!method){
        debug_d("merr\n");
        return NULL;
    }
    //***********************************************************
    method->on_custom_attr_config  = dev_oc_on_custom_attr_config;
    method->on_view_load           = dev_oc_on_view_load;
    method->on_view_did_load       = dev_oc_on_view_did_load;
    method->on_view_will_appear    = dev_oc_on_view_will_appear;
    method->on_view_did_appear     = dev_oc_on_view_did_appear;
    method->on_view_will_disappear = dev_oc_on_view_will_disappear;
    method->on_view_did_disappear  = dev_oc_on_view_did_disappear;
    method->on_view_did_unload     = dev_oc_on_view_did_unload;
    //***********************************************************
    memcpy(&dev_oc->data,dev,sizeof(ui_oc_data_t));
    //***********************************************************
    devinfo->method     = method;    
    devinfo->priv_data  = (void*)dev_oc;
    //***********************************************************

    return devinfo;
}

int dev_oc_unreg(dev_info_t* devinfo)
{
    


    return 0;
}


static void dev_oc_on_page_create(lv_obj_t* parent,void* ui_data)
{
    debug_d("create\n");
    ui_oc_create(parent,ui_data);
}
static void dev_oc_on_page_destroy(void* ui_data)
{
    debug_d("destroy\n");
    ui_oc_destroy(ui_data);
}
static void dev_oc_on_page_update(void* ui_data)
{

}

page_info_t* dev_oc_page_reg(ui_dev_oc_t* oc)
{
    page_method_t*  method  = NULL;
    page_info_t*    page    = NULL;

    page = (page_info_t*)malloc(sizeof(page_info_t));
    if(!page){
        debug_e("err0\n");
        return NULL;
    }
    method = (page_method_t*)malloc(sizeof(page_method_t));
    if(!method){
        debug_e("err1\n");
        return NULL;
    }
    //***********************************************************
    method->on_page_create  = dev_oc_on_page_create;
    method->on_page_destroy = dev_oc_on_page_destroy;
    method->on_page_update  = dev_oc_on_page_update;
    //***********************************************************
    page->method  = method;    
    page->data    = (void*)oc;
    //***********************************************************

    return page;
}